package com.engine.kq.biz;

import com.engine.kq.biz.chain.shiftinfo.ShiftInfoBean;
import com.engine.kq.entity.KQDutyRulesDetailEntity;
import com.engine.kq.entity.KQDutyRulesDetailEntity;
import com.engine.kq.entity.KQOvertimeRulesDetailEntity;
import com.engine.kq.util.KQDurationCalculatorUtil;
import com.engine.kq.wfset.bean.DutyBalanceTimeBean;
import com.engine.kq.wfset.bean.OvertimeBalanceTimeBean;
import com.engine.kq.wfset.bean.SplitBean;
import weaver.common.DateUtil;
import weaver.general.Util;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 根据值班规则计算值班和调休数据
 * 主要是针对 流程+打卡 打卡 这几种方式
 */
public class KQDutyRuleCalBiz {


    /**
     * 根据人和日期获取值班规则里的信息
     *
     * @param resourceid
     * @param fromDate
     * @param toDate
     * @param dateFormatter
     * @param changeTypeMap
     * @param overRulesDetailMap
     * @param restTimeMap
     * @param computingModeMap
     */
    public void getDutyDataMap(String resourceid, String fromDate, String toDate,
                                   DateTimeFormatter dateFormatter, Map<String, Integer> changeTypeMap,
                                   Map<String, KQDutyRulesDetailEntity> overRulesDetailMap, Map<String, List<String[]>> restTimeMap, Map<String, Integer> computingModeMap) {

        LocalDate localFromDate = LocalDate.parse(fromDate);
        LocalDate localToDate = LocalDate.parse(toDate);
        LocalDate preFromDate = localFromDate.minusDays(2);
        LocalDate nextToDate = localToDate.plusDays(1);
        long betweenDays = nextToDate.toEpochDay() - preFromDate.toEpochDay();
        for (int i = 0; i <= betweenDays; i++) {
            LocalDate curLocalDate = preFromDate.plusDays(i);
            String splitDate = curLocalDate.format(dateFormatter);
            KQDutyRulesBiz.getDutyData(resourceid, splitDate, changeTypeMap, overRulesDetailMap, restTimeMap, computingModeMap);
        }
    }


    /**
     * 值班单位
     *
     * @param d_mins
     * @return
     */
    public double getD_MinsByUnit(double d_mins) {
        Map<String, String> map = KQDutyRulesBiz.getMinimumUnitAndConversion();
        if (!map.isEmpty()) {
            double conversionMins = 0.0;
            int minimumUnit = Util.getIntValue(Util.null2String(map.get("minimumUnit")), -1);
            int dutyConversion = Util.getIntValue(Util.null2String(map.get("dutyConversion")), -1);
            if (5 == minimumUnit || 6 == minimumUnit) {
                int halfHourInt = 30;
                int wholeHourInt = 60;
                if (5 == minimumUnit) {
                    conversionMins = getConversionMins(halfHourInt, d_mins, dutyConversion);
                } else {
                    conversionMins = getConversionMins(wholeHourInt, d_mins, dutyConversion);
                }
                return conversionMins;
            }
        }
        return d_mins;
    }




  /**
   * 根据转换规则得到转换后的值班时长
   *
   * @param halfHourInt
   * @param d_mins
   * @param dutyConversion
   * @return
   */
  public double getConversionMins(int halfHourInt, double d_mins, int dutyConversion) {
    double conversionMins = 0.0;
    int step = (int) (d_mins / halfHourInt);
    double leftMins = d_mins - halfHourInt * step;
      //半小时
    if (1 == dutyConversion) {
      //          四舍五入
      if (leftMins >= halfHourInt / 2) {
        conversionMins = halfHourInt * step + halfHourInt;
      } else {
        conversionMins = halfHourInt * step;
      }
    } else if (2 == dutyConversion) {
//          向上取整
      if (leftMins > 0) {
        conversionMins = halfHourInt * step + halfHourInt;
      } else {
        conversionMins = halfHourInt * step;
      }
    } else if (3 == dutyConversion) {
//          向下取整
      if (leftMins < halfHourInt) {
        conversionMins = halfHourInt * step;
      } else {
        conversionMins = halfHourInt * step;
      }
    }
    return conversionMins;
  }

    /**
     * 判断是否开启了调休
     * @param kqDutyRulesDetailEntity
     * @param duty_type
     * @return
     */
    public int getPaidLeaveEnable(KQDutyRulesDetailEntity kqDutyRulesDetailEntity,
                                  String duty_type) {
        int paidLeaveEnable = -1;
        if (kqDutyRulesDetailEntity != null){
            paidLeaveEnable = kqDutyRulesDetailEntity.getPaidLeaveEnable();
            paidLeaveEnable = paidLeaveEnable == 1?1:0;
        }
        int paidLeaveEnableType = kqDutyRulesDetailEntity.getPaidLeaveEnableType();
        if(2 == paidLeaveEnableType){
//      logOvertimeMap(overtimeLogMap, overtime_type, flow_cross_key+"|关联调休与否来自于流程选择,加班类型下拉框值|overtime_type");
            if("0".equalsIgnoreCase(duty_type)){
                paidLeaveEnable = 1;
            }else if("1".equalsIgnoreCase(duty_type)){
                paidLeaveEnable = 0;
            }else{
                paidLeaveEnable = 0;
            }
        }
        return paidLeaveEnable;
    }



}
